package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.dto.ArticleDto;
import com.rockcent.mall.lib.dto.QueryPagingDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Antinomy on 16/8/23.
 */
@Repository
public class ArticleRepositoryImpl implements ArticleRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(ArticleRepositoryImpl.class);

    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<ArticleDto> findArticlePageByMallId(PageResult page,Long mallId,String keyword,Long categoryId,String orderBy, String xsc) {

        SQLPageDoing<ArticleDto> result = (sql, fields, params, typeReferenceMap) -> {

            sql.append(" select %s from article art ");
            fields.append("(select name from article_category cate where cate.id = parent_category_id) as parentCategoryText, ");
            fields.append("(select name from article_category cate where cate.id = category_id) as categoryText, ");
            fields.append("art.* ");

            sql.append("where is_delete = 'NO' ");

            sql.append(" and mall_id = :mallId ");
            params.put("mallId", mallId);


            if (StringUtils.isNotBlank(keyword)) {
                sql.append(" and (art.name like :keyword or art.keyword like :keyword) ");
                params.put("keyword", "%" + keyword.trim() + "%");
            }

            if(categoryId != null){
                sql.append(" and (parent_category_id = :categoryId or category_id = :categoryId)");
                params.put("categoryId", categoryId);
            }
            if (StringUtils.isNotBlank(orderBy )) {
                if (StringUtils.isBlank(xsc)) {
                    sql.append(" order by " + orderBy.trim() + " desc");
                } else {
                    sql.append(" order by " + orderBy.trim() + " " +xsc.trim());
                }
            }
            log.debug(sql.toString());
        };
        return result.go(page, ArticleDto.class, jdbcTemplate, log);
    }

    @Override
    public PageResult<ArticleDto> findArticlePageByCategoryId(PageResult page, Long categoryId,Long mallId) {

        SQLPageDoing<ArticleDto> result = (sql, fields, params, typeReferenceMap) -> {

            sql.append(" select %s from article art ");
            fields.append("(select name from article_category cate where cate.id = parent_category_id) as parentCategoryText, ");
            fields.append("(select name from article_category cate where cate.id = category_id) as categoryText, ");
            fields.append("art.* ");

            sql.append("where is_delete = 'NO' ");

            sql.append(" and mall_id = :mallId ");
            params.put("mallId", mallId);

            sql.append(" and parent_category_id = :categoryId");
            params.put("categoryId", categoryId);

            sql.append(" order by display_order_id ");

            log.debug(sql.toString());
        };


        return result.go(page, ArticleDto.class, jdbcTemplate, log);
    }

    @Override
    public List<ArticleDto> searchArticlByKeyword(Long mallId, String keyword) {
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append("SELECT %s FROM article arc WHERE arc.is_delete='NO' AND arc.name LIKE :keyword AND arc.mall_id=:mallId");
            params.put("keyword","%"+keyword+"%");
            params.put("mallId",mallId);
            fields.append("arc.id,arc.name");
        };
        return doing.go(ArticleDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public List<ArticleDto> getTop5Article(Long mallId) {

        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append("SELECT %s FROM article arc WHERE arc.is_delete='NO' AND arc.mall_id=:mallId limit 0,5");

            params.put("mallId",mallId);
            fields.append("arc.id,arc.name");
        };
        return doing.go(ArticleDto.class, objectMapper, jdbcTemplate, log);

    }
}
